package fichier;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import java.io.File;
import java.sql.*;


public class AfficherModele extends JFrame implements ActionListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2546027855591398278L;
	/**
	 * 
	 */
	private JPanel pane;
	private JLabel designation;
	private JLabel client;
	private JLabel box;
	private JLabel annee;
	private JLabel numero;
	private JLabel numeroPlan;
	private JLabel commentaire;
	private JLabel nbMoules;
	private JLabel nbBoites;
	//private JLabel image;
	private JLabel clientBox ;
	private JLabel boxBox ;
	private JLabel anneeTxt;
	private JLabel designationTxt;
	private JLabel numeroTxt;
	private JLabel numeroPlanTxt;
	private JLabel commentaireTxt;
	private JLabel nbMoulesTxt;
	private JLabel nbBoitesTxt ;
	private JButton modifBut ;
	private JButton supprBut ;
	private JButton genererPdf ;
	private JButton genererPlan ;
	private JMenuBar menuBar;
	private JMenu mModele;
	private JMenu mClient;
	private JMenu fichier;
	private JMenuItem deconnexion;
	private JMenuItem miRechercherM;
	private JMenuItem miAjouterM;
	private JMenuItem miAjouterC;
	public String[] clientboxtab;
	public String[] boxboxtab;
	private File imageFile;
	public static final String pathImage = "/Users/yannlarriau/Documents/workspace/Model Manager/Images/";
	public int iD;

	public AfficherModele(int iD)
	{
		super("Model Manager");
		this.iD=iD;

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		this.setBounds(100,100,550,340 );
		this.setImageFile(new File(pathImage + "imagenonexistante.jpg"));


		SelectData.REQUETE = "select designation_modele, " +
				"nom_client, " +
				"numero_modele, " +
				"numero_plan_modele, " +
				"annee_modele, " +
				"nom_box, " +
				"commentaire_modele, " +
				"nombre_moule_modele, " +
				"nombre_boite_modele " +
				"from modele m " +
				"join client c on m.id_client = c.id_client " +
				"join box b on m.id_box = b.id_box " +
				"where m.id_modele = " + iD;
		ResultSet infoModele = SelectData.startSelect();



		this.menuBar = new JMenuBar();
		this.mModele = new JMenu("Modèle");
		this.mClient = new JMenu("Client");
		this.fichier = new JMenu("Fichier");
		this.deconnexion = new JMenuItem("Déconnexion");
		this.miRechercherM = new JMenuItem("Rechercher");
		this.miAjouterM = new JMenuItem("Ajouter");
		new JMenuItem("Modifier");
		this.miAjouterC = new JMenuItem("Ajouter");

		this.menuBar.add(this.fichier);
		this.menuBar.add(this.mModele);
		this.menuBar.add(this.mClient);
		this.fichier.add(this.deconnexion);
		this.mModele.add(this.miRechercherM);
		this.mModele.add(this.miAjouterM);
		this.mClient.add(this.miAjouterC);
		this.setJMenuBar(this.menuBar);

		this.pane = new JPanel();
		this.designation = new JLabel("Designation :");
		this.client = new JLabel("Client :");
		this.box = new JLabel("N° Box :");
		this.annee = new JLabel("Année :");
		this.numero = new JLabel("Numéro :");
		this.numeroPlan = new JLabel("N° Plan :");
		this.commentaire = new JLabel("Commentaire :");
		this.nbMoules = new JLabel("Nombre de Modèles :");
		this.nbBoites = new JLabel("Nombre de Boites :");
		//this.image = new JLabel("Image");
		Font newLabelFont=new Font(this.designation.getFont().getName(),Font.BOLD,this.designation.getFont().getSize());

		this.designation.setFont(newLabelFont);  
		this.client.setFont(newLabelFont);  
		this.box.setFont(newLabelFont);  
		this.annee.setFont(newLabelFont);  
		this.numero.setFont(newLabelFont);  
		this.numeroPlan.setFont(newLabelFont);  
		this.commentaire.setFont(newLabelFont);  
		this.nbMoules.setFont(newLabelFont);  
		this.nbBoites.setFont(newLabelFont);  

		try {
			if(infoModele.next())
			{
				this.clientBox = new JLabel(infoModele.getString("nom_client"));
				this.boxBox = new JLabel(infoModele.getString("nom_box"));
				this.anneeTxt = new JLabel(infoModele.getString("annee_modele"));
				this.designationTxt = new JLabel(infoModele.getString("designation_modele"));
				this.numeroTxt = new JLabel(infoModele.getString("numero_modele"));
				this.numeroPlanTxt = new JLabel(infoModele.getString("numero_plan_modele"));
				this.commentaireTxt = new JLabel(infoModele.getString("commentaire_modele"));
				this.nbMoulesTxt = new JLabel(infoModele.getString("nombre_moule_modele"));
				this.nbBoitesTxt = new JLabel(infoModele.getString("nombre_boite_modele"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		this.modifBut = new JButton("Modifier");
		this.supprBut = new JButton("Supprimer");
		this.genererPdf = new JButton("Générer PDF");
		this.genererPlan = new JButton("Plan Entrepôt");
		this.pane.setBorder(BorderFactory.createTitledBorder("Ajout de modèle"));


		this.getContentPane().add(this.pane);
		this.pane.add(designation);
		this.pane.add(client);
		this.pane.add(box);
		this.pane.add(annee);
		this.pane.add(numero);
		this.pane.add(numeroPlan);
		this.pane.add(commentaire);
		this.pane.add(nbMoules);
		this.pane.add(nbBoites);
		//this.pane.add(image);
		this.pane.add(designationTxt);
		this.pane.add(clientBox);
		this.pane.add(boxBox);
		this.pane.add(anneeTxt);
		this.pane.add(numeroTxt);
		this.pane.add(numeroPlanTxt);
		this.pane.add(commentaireTxt);
		this.pane.add(nbMoulesTxt);
		this.pane.add(nbBoitesTxt);
		this.pane.add(modifBut);
		this.pane.add(supprBut);
		this.pane.add(genererPdf);
		this.pane.add(genererPlan);

		this.getContentPane().setLayout(null);
		this.pane.setLayout(null);

		this.pane.setBounds(5,5,540,285);
		this.designation.setBounds(30,20,120,20);
		this.client.setBounds(330,20,120,20);
		this.box.setBounds(330,100,120,20);
		this.annee.setBounds(30,100,120,20);
		this.numero.setBounds(30,60,120,20);
		this.numeroPlan.setBounds(330,60,120,20);
		this.commentaire.setBounds(30,140,120,20);
		this.nbMoules.setBounds(30,180,143,20);
		this.nbBoites.setBounds(290,180,130,20);
		//this.image.setBounds(30,260,120,20);
		this.designationTxt.setBounds(150,20,120,20);
		this.numeroTxt.setBounds(150,60,120,20);
		this.numeroPlanTxt.setBounds(410,60,120,20);
		this.boxBox.setBounds(410,100,120,20);
		this.anneeTxt.setBounds(150,100,120,20);
		this.clientBox.setBounds(410,20,120,20);
		this.commentaireTxt.setBounds(150,140,380,20);
		this.nbMoulesTxt.setBounds(175,180,110,20);
		this.nbBoitesTxt.setBounds(420,180,120,20);
		this.modifBut.setBounds(280,240,110,20);
		this.supprBut.setBounds(400,240,110,20);
		this.genererPdf.setBounds(150,240,110,20);
		this.genererPlan.setBounds(30,240,110,20);


		this.deconnexion.addActionListener(this);
		this.modifBut.addActionListener(this);
		this.supprBut.addActionListener(this);
		this.genererPdf.addActionListener(this);
		this.genererPlan.addActionListener(this);
		this.miAjouterC.addActionListener(this);
		this.miRechercherM.addActionListener(this);
		this.miAjouterM.addActionListener(this);


		this.setResizable(false);
		this.setVisible(true);
	}



	@SuppressWarnings("unused")
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == this.modifBut){
			this.dispose();
			ModifierModele modifMod = new ModifierModele(this.iD);
		}
		if(e.getSource()==supprBut)
		{
			int q = JOptionPane.showConfirmDialog(this, "Etes vous sur de vouloir supprimer ce modèle ?", "Attention",JOptionPane.YES_NO_OPTION);
			if(q==0){
				Modele.supprimer(this.iD);
				if(!main.testUpdate)
					JOptionPane.showMessageDialog(this,"Le modèle n'a pas pu être supprime!", "Attention", 1,new ImageIcon(getClass().getResource("/icon/warn.png")));
				else{
					JOptionPane.showMessageDialog(this,"Le modèle a bien été supprimé", "Model Manager", 1,new ImageIcon(getClass().getResource("/icon/ok.png")));
					this.dispose();
					RechercheModele rechMod = new RechercheModele();
				}
			}
		}
		if(e.getSource() == this.miRechercherM){
			this.dispose();

			RechercheModele m = new RechercheModele();
		}
		if(e.getSource() == this.miAjouterM){
			this.dispose();

			AjouterModele maFenetre = new AjouterModele();
		}
		if(e.getSource() == this.miAjouterC){
			this.dispose();

			AjouterClient maFenetre = new AjouterClient();
		}
		if(e.getSource() == this.genererPdf){

			Modele m = new Modele(this.iD);

			PDF p = new PDF(m);
		}
		if(e.getSource() == this.genererPlan){

			PlanFenetre p = new PlanFenetre(this.boxBox.getText());
		}
		if(e.getSource() == this.deconnexion){
			this.dispose();

			Connexion c = new Connexion();	
		}
	}



	public File getImageFile() {
		return imageFile;
	}



	public void setImageFile(File imageFile) {
		this.imageFile = imageFile;
	}


}